if(typeof(SYS)=='undefined')
{
	function $SYS(id)
	{
		if(id == parseInt(id))
		{
			if(id == -1)
			{
				return false;
			}
			for(var i in SYS.blocks)
			{
				if(SYS.blocks[i].id == id)
				{
					return SYS.blocks[i];
				}
			}
			return false;
		}
		var modules = id.split(/\s/);
		var moduleName = modules[modules.length-1];
		var moduleNames = moduleName.split(/:/);
		var index = 0;
		if(moduleNames.length > 1)
		{
			index = parseInt(moduleNames[1]);
		}
		var module = moduleNames[0];
		for(var i in SYS.blocks)
		{
			if(SYS.blocks[i].moduleName == module)
			{
				for(var j = modules.length - 2; j>=0; j--)
				{
					var k = $SYS(SYS.blocks[i].parent_id);
					while(k)
					{
						if(k.name == modules[j])
						{
							break;
						}
						k = $SYS(k.parent_id);
					}
					if(!k)
					{
						break;
					}
				}
				if(j<0 && index-- == 0)
				{
					return SYS.blocks[i];
				}
			}
		}
	}
	SYS = {
		modules:{},
		loadCount:{},
		blocks:{},
		UITheme:'vimua',
		oldHash:location.hash,
		hashs:{},
		version:1,
		lib:{},
		currentBlockId:0,
		maxModuleId:0,
		href:location.href,
		skin:'portals/default/skins/default',
		defaultPortalId:'kultech',
		portalId:'kultech',
		ajaxIndicator:false,
		ready:false,
		actionQueue:[],
		actionQueueRunning:false,
		allOrderedLinks:[],
		type:'',
		init:function()
		{
			if(document.getElementById('lock_overlay')){
				document.body.removeChild(document.getElementById("lock_overlay"));
			}
			$(function(){
				$('body').append('<div id="loading-layer" style="display:none"><div id="loading-layer-text"><img src="'+rootURL+'3rdparty/jQuery/plugins/ajax/images/ajax-loader.gif" align="absmiddle" width="16" height="16" hspace="5" class="displayIn">Đang tải dữ liệu ...</div></div>');
				$('#loading-layer').ajaxStart(function(a,request) {
					if(SYS.ajaxIndicator)
					{
						$(this).attr('top', document.body.scrollTop);
						$(this).show();
					}
				}).ajaxStop(function() {
					$(this).hide();
				});
				if(typeof(SYS) != 'undefined')
				{
					SYS.ajaxIndicator = true;
				}
			});
		},
		makeLinksCache:function(pageName)
		{
			if(SYS.User.isAdmin())
			{
				SYS.addActionQueue({
					isLinkCache:true,
					condition:function(){
						return true;
						for(var i = 0; i < SYS.actionQueue.length; i++)
						{
							if(typeof(SYS.actionQueue[i].isLinkCache)=='undefined' && typeof(SYS.actionQueue[i].run)=='undefined' && !SYS.actionQueue[i].condition.call(this))
							{
								return false;
								break;
							}
						}
						return true;
					},
					success:function(){
						var allLinks = {};
						for(var i in SYS.allOrderedLinks)
						{
							allLinks['links['+i+']'] = SYS.allOrderedLinks[i];
						}

						$.post(this.buildURL({module:'MakeLinksCache', page_name:pageName}), allLinks);
					}
				});
			}
		},
		image:function(url)
		{
			if(typeof(url) == 'string')
			{
				if(/http:\/\//.test(url))
				{
					return url;
				}
				if(/\/upload\//.test(url))
				{
					return SYS.FILE_SERVER_URL+url;
				}
				else
				if(/upload\//.test(url))
				{
					return SYS.FILE_SERVER_URL+'/'+url;
				}
			}
			if(typeof(url) == 'undefined' || url == '' || url == false)
			{
				return rootURL+SYS.skin+'images/no-image.jpg';
			}
			return url;
		},
		request: function(param, defaultValue) {
			if(typeof(defaultValue) == 'undefined')
			{
				defaultValue = '';
			}
		    var regex = '[?&]' + param + '=([^&#]*)';
		    var results = (new RegExp(regex)).exec(SYS.href);
		    if(results) return results[1];
		    return defaultValue;
		},
		buildURL:function(options, urlMode){
			if((typeof(urlMode) == 'undefined' || urlMode == 'ajax') && SYS.urlMode == 'ajax' && typeof(options['page']) != 'undefined')
			{
				options['url_mode'] = 'ajax';
				/*
				if(SYS.portalId == 'estore'){
					options['estore_id'] = SYS.estoreId;
				}*/
				return 'javascript:if(typeof(SYS.Page)!=\'undefined\')SYS.Page.load('+$.toJSON(options).replace(/"/g,"'")+'); void(0);';
			}
			var myRootURL = urlMode?'':rootURL;
			if(typeof(options['page']) != 'undefined' && urlMode != 'href')
			{
			
				switch(options['page'])
				{
				case 'home':
					if(typeof(options['portal_id']) == 'undefined' || options['portal_id'] == SYS.defaultPortalId)
					{
							return myRootURL;
					}
					break;		
			
				case 'product_detail':
					if(typeof(options['id']) != 'undefined')
					{
					    //blacknight's
					    $unsigned_name = ((typeof(options['unsigned_name']) != 'undefined') && (options['unsigned_name'] != null) && (options['unsigned_name'] != '')) ? options['unsigned_name'] : ((typeof(options['name']) != 'undefined')?SYS.removeSign(options['name']):'');
					    return myRootURL+'san-pham/'+((typeof(options['category_id']) != 'undefined')?options['category_id']+'/':'')+options['id']+'_'+$unsigned_name+'.html';
					}
					break;				
				case 'detail':
					if(typeof(options['id']) != 'undefined'){
						return myRootURL+'tin-tuc/'+options['id']+'.tml';
					}					
					break;						
				case 'sign_in':
					return myRootURL+'dang-nhap.html';
					break;		
				case 'sign_in':
					return myRootURL+'dang-nhap.html';
					break;				
				case 'personal':
					return myRootURL+'ca-nhan.html';				
				case 'shop_gift':
					return myRootURL + 'shop-doi-qua.html';
				case 'shop_user_gift':
					return myRootURL + 'danh-sach-nhan-qua.html';
					
				case 'intro':
					return myRootURL + 'dong-dien-thoai-Fmobile.html';
				case 'fpt':
					return myRootURL + 'tap-doan-FPT.html';
				case 'customer':
					return myRootURL + 'loi-ich-khach-hang.html';
					
				case 'listproduct':
					return myRootURL + 'san-pham-moi.html';
				case 'hotproduct':
					return myRootURL + 'san-pham-hot.html';	
					
				case 'myphone':
					return myRootURL + 'my-phone.html';
				case 'directory_myphone':
					return myRootURL + 'my-phone/danh-ba.html';
				case 'space_myphone':
					return myRootURL + 'my-phone/du-lieu.html';
					
				case 'game':
					return myRootURL + 'game.html';
				case 'drawing_game':
					return myRootURL + 'trang-tri-dien-thoai.html';
				case 'vote_game':
					return myRootURL + 'vote-game.html';
					
				case 'promolist':
					return myRootURL + 'khuyen-mai.html';
				case 'hotro':
					return myRootURL + 'ho-tro.html';
				case 'contact':
					return myRootURL + 'lien-lac.html';
				case 'lienhe':
					return myRootURL + 'lien-he.html';
				case 'cuahang':
					return myRootURL + 'cua-hang.html';
				case 'productlist':
					return myRootURL + 'danh-muc/mobile.html';
				case 'newslist':
					return myRootURL + 'tin-tuc/danh-sach.tml';
				case 'emplist':
					return myRootURL + 'tuyen-dung.html';
				//blacknight's
				case 'help' :
					if(typeof(options['id']) != 'undefined')
					{					    
					    return myRootURL+'ho-tro/'+options['id']+'_'+((typeof(options['name']) != 'undefined')? SYS.removeSign(options['name']):'-')+'.html';
					}
					break;
				}									
			}
			if(typeof(options['portal']) == 'undefined')
			{
				options['portal'] = SYS.portalId;
			}
			var url = myRootURL+'?';
			for(var i in options)
			{
				url += '&'+i+'='+encodeURI(options[i]);
			}
			return url;
		},
		cookie:function(name, value)
		{
			if(typeof(value) != 'undefined')
			{
				jQuery.cookie(name, value, {
					domain:'.vimua.com'
				});
			}
			else
			{
				return jQuery.cookie(name);
			}
		},
		xml:function(xmlNode, parentNode)
		{
			var result = {}, count = 0, isArray = false;
			xmlNode.children().each(function(){
				var children = $(this).children(), key;
				if(count == 0 && typeof(result[this.tagName]) == 'undefined' && !isArray)
				{
					if(typeof(parentNode) != 'undefined' && parentNode == this.tagName+'s')
					{
						key = 0;
						isArray = true;
						result = [];
					}
					else
					{
						key = this.tagName;
					}
				}
				else
				{
					if(count == 0 && typeof(result[this.tagName]) != 'undefined')
					{
						result = [result[this.tagName]];
					}
					key = ++count;
				}
				if(children.length > 0)
				{
					result[key] = SYS.xml($(this), key);
				}
				else
				{
					result[key] = $(this).text();
				}
				children = null;
				//result[key]._index = count;
			});

			return result;
		},
		load:function()
		{
			if(typeof(SYS.allLinks) == 'undefined')
			{
				SYS.allLinks = {
					'3rdparty/jQuery/jquery-1.3.2.js':2,
					'3rdparty/jQuery/plugins/jquery.cookie.js':2,
					'3rdparty/jQuery/plugins/jquery.json/jquery.json.min.js':2,
					'lib/js/SYS/SYS.js':2,
					'lib/js/SYS/SYS.User.js':2,
					'lib/js/SYS/SYS.Module.js':2,
					'lib/js/SYS/SYS.Form.js':2,
					'3rdparty/jQuery/plugins/pureJSTemplate.js':2
				};
				SYS.allLinks['publish/'+SYS.portalId+'/modules.js'] = 2;
				for(var i in SYS.allLinks)
				{
					SYS.allOrderedLinks.push(i);
				}
			}
			if(arguments.length>0)
			{
				if(typeof(arguments[0]) == 'function')
				{
					arguments[0].call(this);
				}
				else
				{
					var length = arguments.length;
					if(typeof(arguments[arguments.length - 1]) == 'function')
					{
						length--;
					}
					for(var i = 0; i < length; i++)
					{
						if(typeof(arguments[i]) == 'string')
						{
							SYS.loadURL(arguments[i]);
						}
					}
					if(typeof(arguments[arguments.length - 1]) == 'function')
					{
						SYS.afterLoad.apply(this, arguments);
					}
				}
			}
		},
		loadURL:function(fileName)
		{
			if(fileName.indexOf('.css') != -1)
			{
				if(!SYS.allLinks[fileName])
				{
					SYS.allLinks[fileName] = 1;
					var headID = document.getElementsByTagName("head")[0];
					var cssNode = document.createElement('link');
					cssNode.type = 'text/css';
					cssNode.rel = 'stylesheet';
					cssNode.href = staticURL+fileName+(/\?/.test(fileName)?'':'?')+'v='+SYS.version;
					cssNode.media = 'screen';
					if(headID)
					{
						headID.appendChild(cssNode);
					}
					else
					{
						document.body.appendChild(cssNode);
					}
					headID = cssNode = null;
					//document.styleSheets[0].addImport(staticURL+fileName);
					SYS.allOrderedLinks.push(fileName);
				}
			}
			else
			{
				if(!SYS.allLinks[fileName])
				{
					SYS.allLinks[fileName] = 1;
					$.ajax({
						url: rootURL+fileName+(/\?/.test(fileName)?'':'?')+'v='+SYS.version,
						dataType: 'html',
						cache:true,
						error:function()
						{
							SYS.allLinks[fileName] = 3;
						},
						success:function(data)
						{
							SYS.allLinks[fileName] = 4;

							try {
								eval(data);
							} catch (e) {}
							SYS.allLinks[fileName] = 2;
							SYS.allOrderedLinks.push(fileName);
						}
					});
				}
			}
		},
		addActionQueue:function(action)
		{
			SYS.actionQueue.push(action);
			if(!SYS.actionQueueRunning)
			{
				SYS.execActionQueue();
			}
		},
		execActionQueue:function()
		{
			SYS.actionQueueRunning = true;
			var run = false;
			for(var i = 0; i < SYS.actionQueue.length; i++)
			{
				if(typeof(SYS.actionQueue[i].run)=='undefined')
				{
					run = true;
					if(SYS.actionQueue[i].condition.call(this))
					{
						SYS.actionQueue[i].run = true;
						SYS.actionQueue[i].success.call(this);
					}
				}
			}
			if(run)
			{
				setTimeout('SYS.execActionQueue();',50);
			}
			else
			{
				SYS.actionQueueRunning = false;
				SYS.actionQueue = [];
			}
		},
		afterLoad:function()
		{
			var args = arguments;
			SYS.addActionQueue({
				condition:function(){
					var ok = true;
					for(var i = 0; i < args.length-1; i++)
					{
						if((args[i] && typeof(args[i]) == 'string') && (args[i].indexOf('.css') == -1))
						{
							if(SYS.allLinks[args[i]] != 2)
							{
								if(typeof(SYS.loadCount[args[i]]) == 'undefined')
								{
									SYS.loadCount[args[i]] = 0;
								}
								SYS.loadCount[args[i]] ++;
								if(SYS.loadCount[args[i]] > 200)
								{

									if(SYS.allLinks[args[i]] == 4)
									{
										SYS.allLinks[args[i]] = 2;
									}
								}
								ok = false;
								if(SYS.allLinks[args[i]] == 3)
								{
									return;
								}
							}
						}
					}
					return ok;
				},
				success:function(){
					if(typeof(args[args.length - 1]) == 'function')
					{
						var func = args[args.length - 1];
						func.call(this);

					}
					args = null;
				}
			});
		},
		number:function(st)
		{
			if(typeof(st) != 'undefined' && st != null)
			{
				return parseInt(st.toString().replace(/\./g,''));
			}
			return 0;
		},
		numberFormat:function(number, decimals, dec_point, thousands_sep)
		{
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57
			var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 0 : decimals;
			var d = dec_point == undefined ? "," : dec_point;
			var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
			var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
			return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
		},
		require:function(className, success)
		{
			if(SYS.modules[className])
			{
				SYS.load(SYS.modules[className].path+'/class.js', function(){
					SYS.requireSuccess(className, success);
				});
			}
			else
			{
				SYS.load('lib/js/SYS/'+className+'.js', success);
			}
		},
		requireSuccess:function(className, success)
		{
			SYS.addActionQueue({
				condition:function(){
					return (typeof(SYS[className]) != 'undefined');
				},
				success:function(){
					if(typeof(success) == 'function')
					{
						success.call(this);
					}
				}
			});
		},
		echo:function(st)
		{
			if(SYS.ready)
			{
				$('body').append(st);
			}
			else
			{
				document.write(st);
			}
		},
		using:function(moduleInfo, options, onReady)
		{
			var className = moduleInfo.name;
			if(SYS.modules[className])
			{
				var blockId = moduleInfo.id;

				if(parseInt(SYS.maxModuleId) < parseInt(blockId))
				{
					SYS.maxModuleId = blockId;
				}				
				SYS.load(SYS.modules[className].path+'/class.js',function(){
					if(typeof(SYS.blocks[blockId]) == 'undefined')
					{
						SYS.usingClass(moduleInfo, options, onReady);
					}
				});
			}
			else
			{
				SYS.load('lib/js/SYS/'+className+'.js', onReady);
			}
		},
		moduleReady:function(blockId,onReady)
		{
			SYS.addActionQueue({
				condition:function(){
					return (typeof(SYS.blocks[blockId]) != 'undefined' && SYS.blocks[blockId].isReady);
				},
				success:function(){
					onReady.call(SYS.blocks[blockId]);
				}
			});
		},
		usingClass:function(moduleInfo, options, onReady)
		{
			var className = moduleInfo.name;
			var blockId = moduleInfo.id;
			
			SYS.addActionQueue({
				condition:function(){
					return (typeof(SYS[className]) != 'undefined');
				},
				success:function(){
					eval('var module = new SYS.'+className+'(moduleInfo,options, onReady);');
				}
			});
		},
		extend:function(oldClass, properties)
		{
			var newClass = function()
			{
				if(typeof(oldClass) != 'undefined')
				{
					this.parent = oldClass.prototype;
					oldClass.apply(this,arguments);
				}
			}
			if(typeof(oldClass) != 'undefined')
			{
				$.extend(newClass.prototype, oldClass.prototype, properties);
			}
			return newClass;
		},
		addslashes:function(st)
		{
			if(typeof(st) == 'string')
			{
				return st.replace(/'/g,'\\\'').replace(/"/g,'&quot;');
			}
			return st;
		},
		sizeof:function(obj)
		{
			if(typeof(obj) == 'object')
			{
				var size = 0;
				for(var i in obj)
				{
					if(typeof(obj[i])!='function')
					{
						size ++;
					}
				}
				return parseInt(size);
			}
			else
			if(typeof(obj.length) != 'undefined')
			{
				return obj.length;
			}
			return 0;
		},
		alert:function(message, options)
		{
			SYS.load('3rdparty/jQuery/plugins/jqimpromptu.css');
			SYS.load('3rdparty/jQuery/plugins/jquery.corner.js',
				'3rdparty/jQuery/plugins/jquery-impromptu.1.5.js', function(){
					$.prompt(message,$.extend({ show:'slideDown' }, options));
			});
		},
		parse:function(id, template, params)
		{
			$.fn.pureJSTemplate.setDelimiters("<.", ".>");
			$('#'+id).pureJSTemplate({id:template, data:params});
			//Khoanb Edit 18.05.2009
			if(arguments.length == 4){
				eval(arguments[3]);
			}
		},
		clone:function (myObj)
		{
			if(typeof(myObj) != 'object') return myObj;
			if(myObj == null) return myObj;
			var myNewObj = new Object();
			for(var i in myObj)
				myNewObj[i] = SYS.clone(myObj[i]);
			return myNewObj;
		},
		date:function(time)
		{
			var date=new Date(time * 1000);
			var d = date.getDate()+'/'+(date.getMonth()+1)+'/'+date.getYear();
			delete date;
			return d;
		},
		time:function(time)
		{
			var date=new Date(time * 1000);
			var d = date.getHours()+':'+date.getMinutes()+' '+date.getDate()+'/'+(date.getMonth()+1)+'/'+date.getYear();
			delete date;
			return d;
		},
		
		//blacknight's SEO		
		removeSign: function(str) {
		   str= str.toLowerCase();
		   str= str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g,"a");
		   str= str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g,"e");
		   str= str.replace(/ì|í|ị|ỉ|ĩ/g,"i");
		   str= str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g,"o");
		   str= str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g,"u");
		   str= str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g,"y");
		   str= str.replace(/đ/g,"d");
//		   str= str.replace(/!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'| |\"|\&|\#|\[|\]|~|$|_/g,"-");

		   /* tìm và thay thế các kí tự đặc biệt trong chuỗi sang kí tự - */
		   str = str.replace(/[^a-zA-Z0-9- _]/g,'');		   
//		   str1 = str1.replace(/^\s+/, ''); str1 = str1.replace(/\s+$/, ''); // trim str
		   str = str.replace(/ /g,'-');
		   str= str.replace(/-+-/g,"-"); //thay thế 2- thành 1-
		   str= str.replace(/^\-+|\-+$/g,"");  //cắt bỏ ký tự - ở đầu và cuối chuỗi
		   return str;
		}
	};
	/*
	// Khoanb 06.08.09 - Comment setInterval
	setInterval(function(){		
		if(/\:/.test(location.hash) && location.hash != SYS.oldHash)
		{
			SYS.oldHash = location.hash;
			if(SYS.hashs['#'+SYS.oldHash])
			{
				var page = $SYS('Page');
				$(page)
				{
					page.loadHash('#'+SYS.oldHash);
				}
			}
		}
	}, 500);
	*/
};
String.prototype.ucfirst = function()
{
	var st = this.toString();
	if(st.length > 0)
	{
		return st.charAt(0).toUpperCase()+st.substr(1);
	}
	else
	{
		return '';
	}
};
String.prototype.ucwords = function()
{
	var st = this.toString().replace(/\_/g, ' ');
	var sts = st.split(' ');
	for(var i = 0; i<sts.length; i++)
	{
		sts[i] = sts[i].ucfirst();
	}
	return sts.join('');
};
function is_object (mixed_var){
	var browser = navigator.appName;
	if ($.browser.msie && $.browser.version <= 6) {
		if (mixed_var instanceof Array) {
			return false;
		}
		else {
			return (mixed_var !== null) && (typeof(mixed_var) == 'object');
		}
	}else{
		return true;
	}
}
